package com.liaogui.consumer.service;



import com.liaogui.common.entity.userinfo.UserInfoEntity;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

@Service("passwordService")
public class PasswordService {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

//    @Value(value = "${shiro.credentials.hashAlgorithmName}")
    private String algorithmName = "md5";
//    @Value(value = "${shiro.credentials.hashIterations}")
    private final int hashIterations = 2;

    /**
     * 手机号码加盐加密
     * @param user
     */
    public void encryptPassword(UserInfoEntity user) {
        user.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(algorithmName, user.getPassword(),
                ByteSource.Util.bytes(user.getCredentialsSalt()), hashIterations).toHex();
        user.setPassword(newPassword);
    }

    //开发者密码加密方式
//    public void encryptPassword(UserInfoEntity user){
//        user.setSalt(randomNumberGenerator.nextBytes().toHex());
//        String newPassword = new SimpleHash(algorithmName, user.getPassword(),
//                ByteSource.Util.bytes(user.getSalt()), hashIterations).toHex();
//        user.setPassword(newPassword);
//    }

    //开发者密码加密方式
    public String encryptPassword(String phone,String userSalt,String passWord){
        String newPassword = new SimpleHash(algorithmName, passWord,
                ByteSource.Util.bytes(phone+userSalt), hashIterations).toHex();
        return newPassword;
    }

}

